<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="node302.html" />
<link rel="prev" href="module-bz2.html" />
<link rel="parent" href="module-bz2.html" />
<link rel="next" href="node302.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>12.3.1 (De)compression of files</title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="12.3 bz2  "
  href="module-bz2.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="12.3 bz2  "
  href="module-bz2.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="12.3.2 sequential (de)compression"
  href="node302.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-bz2.html">12.3 bz2  </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-bz2.html">12.3 bz2  </a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="node302.html">12.3.2 Sequential (de)compression</a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h2><a name="SECTION0014310000000000000000">
12.3.1 (De)compression of files</a>
</h2>

<p>
Handling of compressed files is offered by the <tt class="class">BZ2File</tt> class.

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-2338' xml:id='l2h-2338' class="class">BZ2File</tt></b>(</nobr></td>
  <td><var>filename</var><big>[</big><var>, mode</var><big>[</big><var>,
                           buffering</var><big>[</big><var>, compresslevel</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Open a bz2 file. Mode can be either <code>'r'</code> or <code>'w'</code>, for reading 
(default) or writing. When opened for writing, the file will be created if
it doesn't exist, and truncated otherwise. If <var>buffering</var> is given,
<code>0</code> means unbuffered, and larger numbers specify the buffer size;
the default is <code>0</code>. If
<var>compresslevel</var> is given, it must be a number between <code>1</code> and
<code>9</code>; the default is <code>9</code>.
Add a "<tt class="character">U</tt>" to mode to open the file for input with universal newline
support. Any line ending in the input file will be seen as a
"<tt class="character">\n</tt>" in Python.  Also, a file so opened gains the
attribute <tt class="member">newlines</tt>; the value for this attribute is one of
<code>None</code> (no newline read yet), <code>'\r'</code>, <code>'\n'</code>,
<code>'\r\n'</code> or a tuple containing all the newline types
seen. Universal newlines are available only when reading.
Instances support iteration in the same way as normal <tt class="class">file</tt>
instances.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2339' xml:id='l2h-2339' class="method">close</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
Close the file. Sets data attribute <tt class="member">closed</tt> to true. A closed file
cannot be used for further I/O operations. <tt class="method">close()</tt> may be called
more than once without error.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2340' xml:id='l2h-2340' class="method">read</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>size</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Read at most <var>size</var> uncompressed bytes, returned as a string. If the
<var>size</var> argument is negative or omitted, read until EOF is reached.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2341' xml:id='l2h-2341' class="method">readline</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>size</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Return the next line from the file, as a string, retaining newline.
A non-negative <var>size</var> argument limits the maximum number of bytes to
return (an incomplete line may be returned then). Return an empty
string at EOF.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2342' xml:id='l2h-2342' class="method">readlines</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>size</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Return a list of lines read. The optional <var>size</var> argument, if given,
is an approximate bound on the total number of bytes in the lines returned.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2343' xml:id='l2h-2343' class="method">xreadlines</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
For backward compatibility. <tt class="class">BZ2File</tt> objects now include the
performance optimizations previously implemented in the
<tt class="module">xreadlines</tt> module.
<div class="versionnote"><b>Deprecated since release 2.3.</b>
This exists only for compatibility with the method by
                 this name on <tt class="class">file</tt> objects, which is
                 deprecated.  Use <code>for line in file</code> instead.</div><p></p>
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2344' xml:id='l2h-2344' class="method">seek</tt></b>(</nobr></td>
  <td><var>offset</var><big>[</big><var>, whence</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Move to new file position. Argument <var>offset</var> is a byte count. Optional
argument <var>whence</var> defaults to <code>os.SEEK_SET</code> or <code>0</code> (offset from start of file;
offset should be <code>&gt;= 0</code>); other values are <code>os.SEEK_CUR</code> or <code>1</code> (move relative to
current position; offset can be positive or negative), and <code>os.SEEK_END</code> or <code>2</code> (move relative to end
of file; offset is usually negative, although many platforms allow seeking beyond
the end of a file).

<p>
Note that seeking of bz2 files is emulated, and depending on the parameters
the operation may be extremely slow.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2345' xml:id='l2h-2345' class="method">tell</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
Return the current file position, an integer (may be a long integer).
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2346' xml:id='l2h-2346' class="method">write</tt></b>(</nobr></td>
  <td><var>data</var>)</td></tr></table></dt>
<dd>
Write string <var>data</var> to file. Note that due to buffering, <tt class="method">close()</tt>
may be needed before the file on disk reflects the data written.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2347' xml:id='l2h-2347' class="method">writelines</tt></b>(</nobr></td>
  <td><var>sequence_of_strings</var>)</td></tr></table></dt>
<dd>
Write the sequence of strings to the file. Note that newlines are not added.
The sequence can be any iterable object producing strings. This is equivalent
to calling write() for each string.
</dl>

<p>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="12.3 bz2  "
  href="module-bz2.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="12.3 bz2  "
  href="module-bz2.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="12.3.2 sequential (de)compression"
  href="node302.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-bz2.html">12.3 bz2  </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-bz2.html">12.3 bz2  </a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="node302.html">12.3.2 Sequential (de)compression</a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
